package com.itheima.reggie.controlle;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    /**
     * 添加菜品或套餐到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //菜品和套餐id不一定哪一个会请求到服务器 所以要判断非空确保只有一条可以执行
        ShoppingCart cart = shoppingCartService.lambdaQuery()
                //获取当前用户购物车信息
                .eq(ShoppingCart::getUserId, BaseContext.get())
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .orderByDesc(ShoppingCart::getCreateTime).one();

        //添加后判断是否添加过菜品或套餐
        if(cart != null){
            //不等于null则添加1条
            cart.setNumber(cart.getNumber()+1);
            //更新
            shoppingCartService.updateById(cart);
            return R.success(cart);
        }else {
            //如果不在则设置值为一
            //设置字段值
            shoppingCart.setUserId(BaseContext.get());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1); //设置值为1
            shoppingCartService.save(shoppingCart);

            return R.success(shoppingCart);
        }
    }

    /**
     * 显示购物车数据
     */
    @GetMapping("list")
    private R<List<ShoppingCart>> list(){
        //根据用户id查询商品
        List<ShoppingCart> shoppingCartList = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, BaseContext.get())
                .list();
        return R.success(shoppingCartList);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("clean")
    private R<String> clean(){
        shoppingCartService.update()
                .eq("user_id",BaseContext.get()).remove();
        return R.success("清空");
    }

    /**
     * 减一
     */
    @PostMapping("sub")
    private R sub(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart cart = shoppingCartService.lambdaQuery()
                //获取当前用户购物车信息
                .eq(ShoppingCart::getUserId, BaseContext.get())
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .one();
        if(cart.getNumber() == 1){
            //直接删除
            shoppingCartService.removeById(cart);
            return R.success(cart);
        }else {
            cart.setNumber(cart.getNumber()-1);
            shoppingCartService.updateById(cart);
            return R.success(shoppingCart);
        }
    }
}
